// pages/addressAdministration/addressAdministration.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    mesList: [],
    name: [],
    phone: [],
    addr: [],
    add: 1,
    id: 0
  },
  // 编辑页
  edit: function (e) {
    var index = e.currentTarget.dataset.addid;
    // console.log(index)
    wx.navigateTo({
      url: '../addAddress/addAddress?addrId=' + index,
    })
  },
  // 
  // 删除
  delete: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    console.log(id);
    wx.showModal({
      title: '提示',
      content: '确认删除该地址',
      cancelText: "取消",
      confirmText: "确认",
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: 'https://www.xiaozheng8.com/sendUserAddr/delete',
            data: {
              addrId: id
            },
            method: 'post',

            success: function (res) {
              console.log(res);
              that.onLoad()
              console.log("删除地址")
            },
            fail: function (res) { },
            complete: function (res) { },
          })
        }
      }
    })



  },
  content: function (e) {


    var id = e.currentTarget.dataset.index;
    var index = e.currentTarget.dataset.id;
    var that = this;
    this.setData({
      id: index
    })
    wx.setStorageSync("addressId", that.data.id);
    console.log(that.data.id)
    wx.showModal({
      title: '提示',
      content: '确认将该地址设为服务地点',
      success: function (res) {
        if (res.confirm) {
          for (var i = 0; i < that.data.mesList.length; i++) {
            console.log(that.data.mesList[i].addrId);
            if (that.data.mesList[i].addrId == id) {
              wx.setStorageSync("mes", that.data.mesList[i])
            }
          }
          console.log(that.data.add);
          if (that.data.add == null || that.data.add == undefined || that.data.add == "" || that.data.add == 1) {
            wx.navigateBack({

            })
          } else {
            wx.navigateBack({
              delta: parseInt(that.data.add)
            });
          }
        }
      }
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    if (wx.getStorageSync("addressId") == null || wx.getStorageSync("addressId") == undefined || wx.getStorageSync("addressId") == "") {
      that.setData({
        id: 0
      })
    } else {
      that.setData({
        id: wx.getStorageSync("addressId")
      })
    };
    this.request();
    console.log(this.data.mesList);
    console.log(options);

    if (options.add == null || options.add == undefined || options.add == "") { } else {
      that.setData({
        add: options.add
      })
    }
    console.log(that.data.add);
    // var name=wx.getStorageSync("name")
    // var phone=wx.getStorageSync("phone")
    // var addr=wx.getStorageSync("addr")
    // this.setData({
    //   name:name,
    //   phone:phone,
    //   addr:addr
    // })

    // if (wx.getStorageSync("location", res)=="")

  },
  address: function () {

    wx.navigateTo({
      url: '../addAddress/addAddress',
    })
  },

  request: function () {
    var that = this;
    wx.request({
      url: 'https://www.xiaozheng8.com/sendUserAddr/findAll',
      method: "post",
      data: {
        userId: wx.getStorageSync("userId")
      },
      success: function (res) {
        console.log(res)
        that.setData({
          mesList: res.data.data
        })
        console.log(that.data.mesList);
        console.log(that.data.id);
        wx.setStorageSync("mes", that.data.mesList[that.data.id])
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.request();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {
    var that=this;
    this.request();
    // this.onLoad(options);
    // for (var i = 0; i < that.data.mesList.length; i++) {
      // console.log(that.data.mesList[i].addrId);
      // if (that.data.mesList[i].addrId == id) {
        console.log(that.data.mesList);
      
      // }
    // }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})